<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="/web/js/vue2.js"></script>
    <script src="/web/js/axios.min.js"></script>
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>

<div id="app" style="margin: 20px">
    <el-form size="mini" :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px"
             class="demo-dynamic">
        <el-form-item
                v-for="(assist, index) in dynamicValidateForm.assists"
                :key="assist.key"
        >

            <i class="el-icon-loading"></i>
            名称{{index+1}}：
            <el-input style="width: 10%" v-model="assist.title"></el-input>

            <el-divider direction="vertical"></el-divider>

            <i class="el-icon-edit"></i>
            访问地址：
            <el-input style="width: 30%" v-model="assist.url"></el-input>


            <el-button type="danger" @click.prevent="removeDomain(assist)">删除</el-button>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
            <el-button type="warning" @click="addAssist">新增</el-button>
        </el-form-item>
    </el-form>
</div>

<script>
    const vm = new Vue({
        el: "#app",
        data() {
            return {
                dynamicValidateForm: {
                    assists: [{
                        title: '',
                        url: ''
                    }],
                }
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        const param = [];
                        for (let o of this.dynamicValidateForm.assists) {
                            let obj = {title: o.title, url: o.url};
                            console.log(obj);
                            // console.log(o);
                            param.push(JSON.stringify(obj));
                        }
                        console.log(param);
                        alert('submit!');
                        // console.log(this.dynamicValidateForm.assists);

                        axios.post(`/assist/save`, param).then(res => {
                            window.parent.location.reload();
                        })

                        // axios.post(`/assist/save?assists=${param}`)
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            removeDomain(item) {
                this.$confirm("真的确认删除么？").then(_ => {
                    var index = this.dynamicValidateForm.assists.indexOf(item)
                    if (index !== -1) {
                        this.dynamicValidateForm.assists.splice(index, 1)
                    }
                })

            },
            addAssist() {
                this.dynamicValidateForm.assists.push({
                    title: '',
                    url: '',
                    key: Date.now()
                });
            }

        },
        async mounted() {
            let t = await axios.get("/assist/list");
            this.dynamicValidateForm.assists = t.data.data;
        }
    })
</script>
</body>
</html>